package basic.thread.again.demo;

/**生产者消费者问题
 * @author bingo
 * @create 2018-03-06 21:33
 **/
public class Demo1 {

    private Integer MAX_COUNT = 10;

    private  Integer count = 0;

    public synchronized void product(){
        System.out.println(Thread.currentThread().getName()+"开始生产!");
        while (count >= MAX_COUNT){
          try {
              System.out.println("库存已满!停止生产");
              wait();

          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
         count++;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"生产完毕!当前库存"+count);
    }

    public synchronized void consume(){
        System.out.println(Thread.currentThread().getName()+"开始消费!");
        while(count <= 0){
          try {
              System.out.println("我没有存货了!快点生产");
              wait();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }

      }
        count--;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"消费完成!当前库存"+count);
    }

    public static void main(String[] args) {

        Demo1 demo1 = new Demo1();

        new Thread(new Runnable() {
            @Override

            public void run() {
                while(true){
                    demo1.product();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){

                    demo1.product();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){

                    demo1.consume();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){

                    demo1.consume();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){

                    demo1.consume();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
}



}

